package com.daquexian.flexiblerichtextview.span;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

import com.daquexian.flexiblerichtextview.MyConfige;

import java.util.Random;

public class MarkemphasisSpan extends ReplacementSpan {
    private Paint mPaint;
    static Random random = new Random();
    private int mWidth = -1;
    private RectF mRectF = new RectF();

    private int mColor = Color.rgb(0, 0, 0);;

    public MarkemphasisSpan() {
        initPaint();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.rgb(0, 0, 0));
        mPaint.setAntiAlias(true);
    }


    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        //return text with relative to the Paint
        mWidth = (int) paint.measureText(text, start, end);
        return mWidth;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        float charx = x;
        for(int i = start ; i<end; i++) {
            String charAt = extractText(text, i, i + 1);
            float charWidth = paint.measureText(charAt);
            //mRectF.set(charx, top, charx += charWidth, bottom);
            mPaint.setColor(mColor);
            //根据每个字的位置绘制背景
            //canvas.drawOval(mRectF, mPaint);
            //canvas.drawPoint(x + charWidth/2, bottom - 2, mPaint);//画一个点
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            float height1 = fontMetrics.descent - fontMetrics.ascent;
            float height2 = fontMetrics.bottom - fontMetrics.top;

            canvas.drawCircle(charx + charWidth/2, top + height2 + 10, 2, mPaint);
            //canvas.drawCircle(charx + charWidth/2, bottom - MyConfige.lineSpacingExtra, 2, mPaint);
            charx += charWidth;
        }
        //绘制字体，如果不掉用这个函数，就不会显示字体啦。
        canvas.drawText(text, start, end, x, y, paint);
    }

    private String extractText(CharSequence text, int start, int end) {
        return text.subSequence(start, end).toString();
    }
}
